package ja.lingo.application;

import ja.centre.gui.concurrent.ATask;
import ja.centre.gui.concurrent.IThrowableProcessor;
import ja.centre.gui.concurrent.TaskSequence;
import ja.centre.util.measurer.TimeMeasurer;
import ja.lingo.application.model.Preferences;
import ja.lingo.application.model.Skeleton;
import ja.lingo.application.util.misc.LogInitializer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Manusovich Alexander
 * @version $Id:$
 */
public class JELearn {
    private static final Log log = LogFactory.getLog(JELearn.class);
    private Skeleton skeleton;

    public JELearn() {
        try {
            final TimeMeasurer measurer = new TimeMeasurer();

            TaskSequence taskSequence = new TaskSequence(new IThrowableProcessor() {
                public void process(Throwable t) {
                    processThrowableAndExit(t);
                }
            }).addTask(new ATask("initialize skeleton") {
                public void run() throws Exception {
                    // few hacks
                    Preferences prefs = Preferences.load();
                    skeleton = new Skeleton(prefs);
                }
            }).addGuiTask(new ATask("hide splash, show skeleton") {
                public void run() throws Exception {
                    skeleton.show();
                }
            });
            taskSequence.runAndWaint();

            log.info("Started within " + measurer);
        } catch (Throwable t) {
            processThrowableAndExit(t);
        }
    }

    private void processThrowableAndExit(Throwable t) {
        log.error("Error occured during start");
        System.exit(1);
    }

    public static void main(String[] args) {
        LogInitializer.initialize();
        new JELearn();
    }
}
